package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eUnionActivityGroupType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.ActivityInfoMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

public class UnionActivityGroupModule extends GeneralModule {

    public UnionActivityGroupModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void loginSendMsg() {
        //6.4.0优化前提
        syncAble();
    }

    @Override
    public void afterLogin() {
//        syncAble();
    }

    public int clientGetAble(int activityId){
        //用这个方法拿 不然盐场啥的拿不到
        ActivityInfo info = ActivityMgr.getOpenActivityInfo(activityId);
        if(info == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if (ActivityMgr.activityInShowTime(info)) {
            int groupId = GameUnionActivityGroupMgr.getGroupId(info.getActivityId(), getUserId());
            UnionActivityGroupProto.UnionActivityGroupAbleMsg.Builder ableBuilder = UnionActivityGroupProto.UnionActivityGroupAbleMsg.newBuilder();
            ableBuilder.setActivityId(info.getActivityId());
            ableBuilder.setAble(groupId != UnionActivityGroupHelper.NO_GROUP_ID);

            eUnionActivityGroupType type = eUnionActivityGroupType.getType(info.getType());

            UnionActivityGroupUnion groupUnion = GameUnionActivityGroupMgr.getUnionData(info.getActivityId(), getUserId());
            if(groupUnion != null){
                if(type != null && type.isLockMember()){
                    if(groupUnion.getLocked() == 0){
                        //修正无资格商会
                        ableBuilder.setAble(false);
                    }
                }
            }else {
                if (checkIsNeedUnionActivity(info.getType())) {
                    ableBuilder.setAble(false);
                }
            }

            String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, getUserId());
            ableBuilder.setUnionUid(unionUid);
            UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityId, unionUid);
            if (unionData != null) {
                ableBuilder.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(unionData.getUnionBaseInfo()));
            }


            UnionActivityGroupProto.UnionActivityGroupClientGetAbleRespMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupClientGetAbleRespMsg.newBuilder();
            builder.setRet(0);
            builder.setMsg(ableBuilder);
            player.sendPacket(ClientProtocol.U_UNION_ACTIVITY_GROUP_CLIENT_GET_ABLE, builder);
            return 0;
        }else {
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }
    }

    public void syncAble() {

        UnionActivityGroupProto.UnionActivityGroupSyncAbleRespMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupSyncAbleRespMsg.newBuilder();

        for (int activityId : GameUnionActivityGroupMgr.getGroupMap().keySet()) {
            ActivityInfo info = ActivityInfoMgr.getActivityInfo(activityId);
            if (ActivityMgr.activityInShowTime(info)) {

                int groupId = GameUnionActivityGroupMgr.getGroupId(info.getActivityId(), getUserId());

                UnionActivityGroupProto.UnionActivityGroupAbleMsg.Builder ableBuilder = UnionActivityGroupProto.UnionActivityGroupAbleMsg.newBuilder();
                ableBuilder.setActivityId(info.getActivityId());
                ableBuilder.setAble(groupId != UnionActivityGroupHelper.NO_GROUP_ID);

                eUnionActivityGroupType type = eUnionActivityGroupType.getType(info.getType());

                UnionActivityGroupUnion groupUnion = GameUnionActivityGroupMgr.getUnionData(info.getActivityId(), getUserId());
                if(groupUnion != null){
                    if(type != null && type.isLockMember()){
                        if(groupUnion.getLocked() == 0){
                            //修正无资格商会
                            ableBuilder.setAble(false);
                        }
                    }
                }else {
                    if (checkIsNeedUnionActivity(info.getType())) {
                        ableBuilder.setAble(false);
                    }
                }

                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, getUserId());
                ableBuilder.setUnionUid(unionUid);
                UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityId, unionUid);
                if (unionData != null) {
                    ableBuilder.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(unionData.getUnionBaseInfo()));
                }

                builder.addMsg(ableBuilder);
            }
        }



        player.sendPacket(ClientProtocol.U_UNION_ACTIVITY_GROUP_SYNC_ABLE, builder);
    }

    public boolean checkIsNeedUnionActivity(int activityType){
        if (activityType == eActivityType.HorseRun.getValue()){
            return true;
        }
        return false;
    }


    public void syncOneAble(int activityId, boolean able) {
        UnionActivityGroupProto.UnionActivityGroupSyncAbleRespMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupSyncAbleRespMsg.newBuilder();
        UnionActivityGroupProto.UnionActivityGroupAbleMsg.Builder ableBuilder = UnionActivityGroupProto.UnionActivityGroupAbleMsg.newBuilder();
        ableBuilder.setActivityId(activityId);
        ableBuilder.setAble(able);
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, getUserId());
        ableBuilder.setUnionUid(unionUid);
        UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityId, unionUid);
        if (unionData != null) {
            ableBuilder.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(unionData.getUnionBaseInfo()));
        }
        builder.addMsg(ableBuilder);
        player.sendPacket(ClientProtocol.U_UNION_ACTIVITY_GROUP_SYNC_ABLE, builder);
    }

}
